package portBroadcasting;

import WhiskeyGui.DataCache;

import java.net.*;
import java.lang.*;
import java.text.DecimalFormat;
import java.util.Date;


/**
 * User: Mike
 * Date: 5/26/12
 */

/**
 * This is the main class to broadcast fake temperature data from.
 * fake temperature data is read in from a file
 * compile this to MultiCastServer_v1.0.jar
 *
 * Multicast on my router
 * IGMP Multicast memberships
 * Multicast Group Address
 * 224.0.0.253
 * 224.0.0.251
 * 239.255.255.250
 * 224.0.0.252
 * 239.192.152.143
 * 224.5.6.74
 */

// todo write in a cmd line call to record all data to file & stop it & delete the file after it gets larger than some value..
// todo write in a cmd line call to clear the data file & start recording it again.

public class FakeServer {

    private static final int MULTI_PORT = 1200;                     // port number should be more than 1024
    private static final String I_NET_ADD = "224.224.224.224";        // address between 224.0.0.1 to 239.255.255.255
    private static final int SECONDS_TO_SLEEP = 1000;       // every 1 sec = 1000 ms

    private static DataCache dataCache;


    public static void main(String args[]) throws Exception {
        // read in data
        dataCache = new DataCache("C:\\WhiskeyData\\fakeTempData.txt");
        dataCache.setTimeSpan("30 days");
        dataCache.Update();
        // start sending data out
        MulticastFakeData();
    }

    private static void MulticastFakeData() throws Exception {
        MulticastSocket socket;
        DatagramPacket packet;
        InetAddress address = InetAddress.getByName(I_NET_ADD);
        socket = new MulticastSocket();
        socket.joinGroup(address);                          // join a Multicast group and send the group salutations
        byte[] data;

        int locationInDataFile = 0;

        double milliTimeD;
        long milliTime;
        double columnDC;
        double columnDF;
        double ambientDC;
        double ambientDF;
        ambientDC = 22.;
        ambientDF = 72.;

        System.out.println(dataCache.getmilliTime().size());

        while (locationInDataFile < dataCache.getmilliTime().size()) {
            milliTimeD = dataCache.getmilliTime().get(locationInDataFile);
            milliTime = (long) milliTimeD;
            columnDC = dataCache.getColumnC().get(locationInDataFile);
            columnDF = columnDC * 9.0 / 5.0 + 32.0;
            DecimalFormat df = new DecimalFormat("###.##");

            columnDC = Double.valueOf(df.format(columnDC));
            columnDF = Double.valueOf(df.format(columnDF));

            String str = milliTime + "," + columnDC + "," + columnDF + "," + ambientDC + "," + ambientDF;
            data = str.getBytes();
            packet = new DatagramPacket(data, str.length(), address, MULTI_PORT);
            socket.send(packet);                            // Sends the packet
            System.out.println("time = " + new Date((long) milliTime) + " ::: Column = (" + columnDC + "," + columnDF + ") Ambient = (" + ambientDC + "," + ambientDF + ")");
            locationInDataFile++;

            Thread.sleep(SECONDS_TO_SLEEP);                             // every 1 sec = 1000 ms
        }
    }
}